package com.leetcode;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Z-hong on 2023/3/7.
 */

public class LeetCode15 {
    public List<List<Integer>> threeSum(int[] nums) {
        if (nums.length<3)return null;
        Arrays.sort(nums);
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        for (int i = 0; i < nums.length-2; i++) {
            for (int j = i+1; j < nums.length-1; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if (nums[i]+nums[j]+nums[k]==0){
                        ArrayList<Integer> objects = new ArrayList<>();
                        objects.add(nums[i]);
                        objects.add(nums[j]);
                        objects.add(nums[k]);
                        objects.sort(Comparator.naturalOrder());
                        if (lists.contains(objects))continue;
                        lists.add(objects);
                    }
                }
            }
        }
        return lists;
    }

    @Test
    public void test(){
    int[] nums={-1,0,1,2,-1,-4};
    System.out.println(threeSum(nums).toString());
    }
}
